"""
@author: mark
@contact: QQ:11241642375
@Created on: 2024年8月17日18:07:04
@Remark: 序列化器
"""
from yaml import serialize

from sales.models import SaleField, SaleData
from dvadmin.utils.serializers import CustomModelSerializer
from rest_framework import serializers


class SaleFieldSerializer(CustomModelSerializer):
    """
    序列化器
    """
    class Meta:
        model = SaleField
        fields = "__all__"


class SaleFieldCreateUpdateSerializer(CustomModelSerializer):
    """
    创建/更新时的列化器
    """

    class Meta:
        model = SaleField
        fields = '__all__'


class SaleFieldResourceSerializer(CustomModelSerializer):
    """
    配置导出序列化器
    """
    field_category = serializers.CharField(source='get_field_category_display', read_only=True, help_text="字段分类")
    calculation_type = serializers.CharField(source='get_calculation_type_display', read_only=True, help_text="计算类型")
    creator = serializers.CharField(source='creator.name', default=None, read_only=True, help_text='创建人')
    modifier = serializers.CharField(default=None, read_only=True, help_text='修改人')

    class Meta:
        model = SaleField
        fields = '__all__'

class SaleDataSerializer(CustomModelSerializer):
    """
    普通序列化起
    """
    # 获取外键序列化的名字
    department_name = serializers.CharField(source="department.name", read_only=True)

    class Meta:
        model = SaleData
        fields = "__all__"


class SaleDataCreateUpdateSerializer(CustomModelSerializer):
    """
    普通序列化起
    """

    class Meta:
        model = SaleData
        fields = "__all__"


class SaleDataResourceSerializer(CustomModelSerializer):
    department = serializers.CharField(source='department.name', default=None, read_only=True, help_text='部门')
    sale_time = serializers.DateField(default=None, read_only=True, help_text="销售日期")
    sale_data = serializers.JSONField(default=None, read_only=True, help_text="销售数据")
    creator = serializers.CharField(source='creator.name', default=None, read_only=True, help_text='创建人')
    modifier = serializers.CharField(default=None, read_only=True, help_text='修改人')

    class Meta:
        model = SaleData
        fields = "__all__"